<?php
require_once 'DAL/DA_Canciones.php';
require_once 'DAL/DA_Repertorios.php';
require_once 'BE/BERepertorio_Cancion.php';

$repertorio_id = 0;
$index = 0;
$cancion_id = 0;
$repertorio = new BERepertorio();
$cancion = new BECancion();
$exitURL = "";

if (isset($_GET["id"])) {//MODO REPERTORIO?
    if ($_GET["id"] > 0) {//MODO REPERTORIO
        $repertorio_id = $_GET["id"];
        $repertorio = DA_Repertorios::ObtenerRepertorioDeID($repertorio_id);

        $exitURL = "repertorios.php?id=$repertorio_id&qr=%";

        if (isset($_GET["index"]))
            $index = $_GET["index"];

        $cancion_de_repertorio = new BERepertorio_Cancion();

        foreach ($repertorio->canciones as $cancion_de_repertorio) {
            if ($cancion_de_repertorio->Orden == $index) {
                $cancion = $cancion_de_repertorio->Cancion;
                $momento = $cancion_de_repertorio->Momento;
            }
        }
    } else { //REPERTORIO ID INVALIDO
        //TODO: Add logic for invalid songs list.
    }
} else if (isset($_GET["cancion_id"])) {
    $cancion_id = $_GET["cancion_id"];
    $cancion = DA_Canciones::obtenerCancion($cancion_id);
    $exitURL = "editor_canciones.php?id=$cancion_id";
}


$maxIndex = count($repertorio->canciones) - 1;
$editURL = "editor_canciones.php?id=$cancion->cancion_id";
$nextIndex = $index + 1;
$prevIndex = $index - 1;

if ($index >= $maxIndex) $nextIndex = 0;
if ($index <= 0) $prevIndex = $maxIndex;

$prevURL = "ejecutar_repertorio.php?id=$repertorio_id&index=$prevIndex";
$nextURL = "ejecutar_repertorio.php?id=$repertorio_id&index=$nextIndex";
$reloadURL = "ejecutar_repertorio.php?id=$repertorio_id&index=$index";

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Ejecutar Repertorio</title>
<?php include 'include/common_html_head.php' ?>
        <style type="text/css" >
            *{
                color:yellow;
                background-color:black;
            }
            body{
                background-color:black;
                color:yellow
            }
            .divNews{
                position:absolute;
                top:60px;
                left:5px;
                width:950px;
                clip:rect(0px,1202px,5000px,0px);
                border-width:2px;
                border-style: solid;
                border-color:navy;
                font-family: monospace;
                font-size: 2.5em;

            }
        </style>
        <script language="javascript" type="text/javascript">
            var topedge;
            var leftedge;
            var boxheight;
            var boxwidth;
            var scrollheight;
            var delay;
            var tiempo_inicio;

            $(document).ready(function(){
                topedge = 60; //Location of the news box from top of page
                leftedge = 5; //Location of news box from left edge
                boxheight = 800;  // height of news box
                boxwidth = 1202;  // width of news box
                scrollheight = $("#news").css("height"); // total height of all data to be scrolled
                scrollheight = scrollheight.substr(0, scrollheight.length - 2);
                var durMin = $("#hidDurMin").val();
                var introDurSec = <?php echo $cancion->intro_dur_sec ?>;
                var durSec = durMin*60;
                var durMillisec = durSec*1000;
                var pixelsToGo = scrollheight;
                delay = durMillisec / pixelsToGo;
                $("#spnDelayText").html(introDurSec +"s->"+ durMin+"m");
            });

            function ejecutarCancion(){
                $("#cmdIniciar").css("background-color","red");
                $("#cmdIniciar").val("3-2-1...");
                $("#cmdIniciar").attr("disabled","disabled");
                setTimeout("delayForIntro()",3000);
            }

            function delayForIntro(){
                var introDurMillisec = <?php echo $cancion->intro_dur_sec ?> * 1000;
                $("#cmdIniciar").css("background-color","#a0a0ff");
                $("#cmdIniciar").val("Intro...");
                setTimeout("iniciarCancion()",introDurMillisec);
            }

            function iniciarCancion(){
                tiempo_inicio = new Date();
                $("#cmdIniciar").css("background-color","#a0a0ff");
                
                scrollnews(0);
            }
            
            function scrollnews(cliptop) {
                
                var duracion = new Date() - tiempo_inicio;
                var durMin = $("#hidDurMin").val();
                durMin = durMin*60;
                duracion = duracion/1000;
                duracion = Math.round(duracion);
                $("#cmdIniciar").val("Cantando...");
                $("#spnDelayText").html(duracion + "s of " + durMin );
                newsDiv = document.getElementById('news').style;
                newsDiv.clip = "rect(" + cliptop + "px " + (boxwidth + leftedge) + "px " + (cliptop + boxheight) + "px 0px)";
                newsDiv.pixelLeft = leftedge;
                newsDiv.pixelTop = topedge - cliptop;

                cliptop = (cliptop + 1) % (scrollheight + boxheight);
                setTimeout("scrollnews(" + cliptop + ")",delay);
            }

            function irAPrev(){
                window.location = '<?php echo $prevURL ?>';
            }

            function cmdSalir_onclick(){
                window.location = '<?php echo $exitURL ?>';
            }

            function irASiguiente(){
                window.location = '<?php echo $nextURL ?>';
            }

            function recargar(){
                window.location = '<?php echo $reloadURL ?>';
            }
            //  End -->

        </script>
    </head>
    <body>

        <span style="font-size:18px!important;font-family:verdana">
            <input style="font-size:1.5em;" type="button" id="cmdPREV" onclick="irAPrev();" value="Previa" />
            <input style="font-size:1.5em;" type="button" id="cmdRESET" onclick="recargar();" value="Recargar" />
            <input style="font-size:1.5em;" type="button" id="cmdNEXT" onclick="irASiguiente();" value="Siguiente" />
        </span>
        <input id="cmdIniciar" style="font-size:2em;background-color:lime" type="button" onclick ="ejecutarCancion();" value="INICIAR">
        <span id="spnDelayText" style="font-size:1em">TBD</span>
        <input style="font-size:1.5em;" type="button" id="cmdNEXT" onclick="cmdSalir_onclick();" value="Salir" />
        <div ID="news" class="divNews">
            <input type="hidden" id="hidDurMin" value="<?php echo $cancion->dur_min; ?>"/>
            <fieldset style="font-size:1em;font-family:'Courier New',Courier,monospace;">
<?php echo str_ireplace("\n", "<br />", str_ireplace(" ", "&nbsp;", htmlspecialchars($momento . " - " . $cancion->titulo))); ?>
                <a href="<?php echo $editURL; ?>" target="_blank">Editar</a>
            </fieldset>
            <fieldset style="font-size:1em;font-family:'Courier New',Courier,monospace;">
<?php echo str_ireplace("\n", "<br />", str_ireplace(" ", "&nbsp;", htmlspecialchars($cancion->introduccion))); ?>
            </fieldset>
            <fieldset style="font-size:1em;font-family:'Courier New',Courier,monospace;">
<?php echo str_ireplace("\n", "<br />", str_ireplace(" ", "&nbsp;", htmlspecialchars($cancion->cancion_y_notas))); ?>
            </fieldset>
        </div>
    </body>
</html>
